package utils;

import model.StudentClass;

import java.util.ArrayList;
import java.util.List;

public class PageUtils {

    private Integer pageNumb;   //当前页
    private Integer pageSize;   //每页条数

    private Integer rowCount;   //总的条数
    private Integer pageCount;    //总的页数
    private Integer prePageNumb;   //前一页数
    private Integer nextPageNumb;   //后一页数
    private List<StudentClass> currentList;   //当前需要展示的数据集合

    public PageUtils(Integer pageNumb, Integer pageSize, ArrayList<StudentClass> allList){
        //初始化每页的内容条数
        this.pageSize = pageSize;

        //初始化总共的内容条数
        this.rowCount = allList.size();

        //计算共有多少页
        if (this.rowCount % this.pageSize == 0) {
            this.pageCount = this.rowCount / this.pageSize;
        } else {
            this.pageCount = this.rowCount / this.pageSize + 1;
        }

        //修正当前页
        if (pageNumb <= 0) {
            this.pageNumb = 1;
        } else if (pageNumb > this.pageCount) {
            this.pageNumb = this.pageCount;
        } else {
            this.pageNumb = pageNumb;
        }

        //处理前一页和后一页的页码
        this.prePageNumb = this.pageNumb - 1;
        if (this.prePageNumb <= 0) {
            this.prePageNumb = 1;
        }
        this.nextPageNumb = this.pageNumb + 1;
        if (this.nextPageNumb > this.pageCount) {
            this.nextPageNumb = this.pageCount;
        }

        //处理当前页的记录开始结束位置
        Integer fromIndex = (this.pageNumb - 1) * pageSize;
        Integer toIndex = this.pageNumb *pageSize;
        if (toIndex > this.rowCount) {
            toIndex = this.rowCount;
        }
        this.currentList =allList.subList(fromIndex, toIndex);
    }

    public Integer getPageCount() {
        return pageCount;
    }

    public Integer getPageNumb() {
        return pageNumb;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public Integer getRowCount() {
        return rowCount;
    }

    public Integer getPrePageNumb() {
        return prePageNumb;
    }

    public Integer getNextPageNumb() {
        return nextPageNumb;
    }

    public List getCurrentList() {
        return currentList;
    }

}
